package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StoreDAO {
    public List<Store> getAllStores() {
        List<Store> stores = new ArrayList<>();
        try (Connection connection = DatabaseConnection.getConnection()) {
            String query = "SELECT * FROM Stores";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            while (resultSet.next()) {
                stores.add(new Store(
                        resultSet.getInt("StoreID"),
                        resultSet.getString("StoreName"),
                        resultSet.getString("StoreLocation"),
                        resultSet.getString("ContactNumber")
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stores;
    }


    public void addStore(Store store) {
        try (Connection connection = DatabaseConnection.getConnection()) {
            String query = "INSERT INTO Stores (StoreName, StoreLocation, ContactNumber) VALUES (?, ?, ?)";
            PreparedStatement preparedStatement = connection.prepareStatement(query);
            preparedStatement.setString(1, store.getStoreName());
            preparedStatement.setString(2, store.getStoreLocation());
            preparedStatement.setString(3, store.getContactNumber());
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
